package nm.aleksey.client;

import com.google.gwt.core.client.GWT;
import com.google.gwt.core.client.GWT.UncaughtExceptionHandler;
import com.google.gwt.user.client.ui.LayoutPanel;
import com.google.web.bindery.event.shared.UmbrellaException;
import com.google.web.bindery.requestfactory.gwt.client.RequestFactoryLogHandler;
import com.google.web.bindery.requestfactory.shared.LoggingRequest;
import nm.aleksey.client.widgets.Table;

import java.util.ArrayList;
import java.util.logging.Level;
import java.util.logging.Logger;

public class App {
  private static final Logger log = Logger.getLogger(App.class.getName());

  private final ClientFactory clientFactory;

  public App(ClientFactory clientFactory) {
    this.clientFactory = clientFactory;
  }

  /**
   * Given a parent view to show itself in, start this App.
   * 
   * @param parentView
   *          where to show the app's widget
   */
  public void run(LayoutPanel parentView) {
    GWT.setUncaughtExceptionHandler(new UncaughtExceptionHandler() {
      @Override
      public void onUncaughtException(Throwable e) {
        while (e instanceof UmbrellaException) {
          e = ((UmbrellaException) e).getCauses().iterator().next();
        }

        String message = e.getMessage();
        if (message == null) {
          message = e.toString();
        }
        log.log(Level.SEVERE, "Uncaught exception", e);
        // Window.alert("An unexpected error occurred: " + message);
      }
    });

    RequestFactoryLogHandler.LoggingRequestProvider provider = new RequestFactoryLogHandler.LoggingRequestProvider() {
      @Override
      public LoggingRequest getLoggingRequest() {
        return clientFactory.getRequestFactory().loggingRequest();
      }
    };
    // Logger.getLogger("").addHandler();
    Logger.getLogger("").addHandler(
        new RequestFactoryLogHandler(provider, Level.ALL,
            new ArrayList<String>()));

    AuthorEditorWorkflow.register(clientFactory);
//    BookEditorWorkflow.register(clientFactory);
    Table table = new Table(clientFactory);
    parentView.add(table);
  }

  // public void simpleTest(LayoutPanel parentView) {
  // SimpleEventBus eventBus = new SimpleEventBus();
  // final TableRequestFactory requestFactory = GWT
  // .create(TableRequestFactory.class);
  // requestFactory.initialize(eventBus);
  //
  // Label authorLabel = new Label();
  // authorLabel.setText("Author name:");
  // final Label authorNameLabel = new Label();
  // final TextBox authorIdTextBox = new TextBox();
  // authorIdTextBox.setText("1");
  // final TextBox authorNewNameTextBox = new TextBox();
  //
  // Button addAuthorButton = new Button();
  // addAuthorButton.setText("Add author");
  // addAuthorButton.addClickHandler(new ClickHandler() {
  //
  // @Override
  // public void onClick(ClickEvent event) {
  // AuthorContext authorContext = requestFactory.authorRequest();
  // AuthorProxy author = authorContext.create(AuthorProxy.class);
  // String name = authorNewNameTextBox.getText();
  // author.setName(name);
  // author.setEmail("com.com.com");
  // authorContext.save(author).fire(new Receiver<Void>() {
  //
  // @Override
  // public void onSuccess(Void response) {
  // authorNewNameTextBox.setText("");
  // }
  //
  // });
  // }
  //
  // });
  //
  // Button editAuthorButton = new Button();
  // editAuthorButton.setText("Select author");
  // editAuthorButton.addClickHandler(new ClickHandler() {
  //
  // @Override
  // public void onClick(ClickEvent event) {
  // AuthorContext authorContext = requestFactory.authorRequest();
  // Long id = Long.parseLong(authorIdTextBox.getText());
  // authorContext.find(id).fire(new Receiver<AuthorProxy>() {
  //
  // @Override
  // public void onSuccess(AuthorProxy response) {
  // authorNameLabel.setText(response.getName() + " version: "
  // + response.getVersion());
  // }
  //
  // });
  // }
  //
  // });
  //
  // Button listAuthorsButton = new Button();
  // listAuthorsButton.setText("List author");
  // listAuthorsButton.addClickHandler(new ClickHandler() {
  //
  // @Override
  // public void onClick(ClickEvent event) {
  // AuthorContext authorContext = requestFactory.authorRequest();
  // authorContext.findAll().fire(new Receiver<List<AuthorProxy>>() {
  //
  // @Override
  // public void onSuccess(List<AuthorProxy> response) {
  // String result = "";
  // for (AuthorProxy author : response) {
  // result += " " + author.getName();
  // }
  // Window.alert(result);
  // }
  //
  // });
  // }
  //
  // });
  //
  // Button mergeAuthorButton = new Button();
  // mergeAuthorButton.setText("Merge author");
  // mergeAuthorButton.addClickHandler(new ClickHandler() {
  //
  // @Override
  // public void onClick(ClickEvent event) {
  // AuthorContext authorContext = requestFactory.authorRequest();
  // Long id = Long.parseLong(authorIdTextBox.getText());
  // authorContext.find(id).fire(new Receiver<AuthorProxy>() {
  //
  // @Override
  // public void onSuccess(AuthorProxy response) {
  // AuthorContext authorContext = requestFactory.authorRequest();
  // AuthorProxy author = authorContext.edit(response);
  // String newName = authorNewNameTextBox.getText();
  // author.setName(newName);
  // authorContext.save(author).fire(new Receiver<Void>() {
  //
  // @Override
  // public void onSuccess(Void response) {
  // authorNewNameTextBox.setText("");
  // }
  //
  // });
  // }
  //
  // });
  // }
  //
  // });
  //
  // EntityProxyChange.registerForProxyType(eventBus, AuthorProxy.class,
  // new EntityProxyChange.Handler<AuthorProxy>() {
  // @Override
  // public void onProxyChange(EntityProxyChange<AuthorProxy> event) {
  // EntityProxyId<AuthorProxy> authorId = event.getProxyId();
  // requestFactory.find(authorId).fire(new Receiver<AuthorProxy>() {
  // @Override
  // public void onSuccess(AuthorProxy author) {
  // authorNameLabel.setText(author.getName() + " version: "
  // + author.getVersion());
  // }
  // });
  // }
  // });
  //
  // HorizontalPanel panel = new HorizontalPanel();
  // panel.setSpacing(10);
  // panel.add(authorIdTextBox);
  // panel.add(editAuthorButton);
  // panel.add(addAuthorButton);
  // panel.add(listAuthorsButton);
  // panel.add(authorNewNameTextBox);
  // panel.add(mergeAuthorButton);
  // panel.add(authorLabel);
  // panel.add(authorNameLabel);
  // parentView.add(panel);
  // }
}
